<template>
  <div class="layout-padding">
    <div class="layout-padding-auto layout-padding-view">

      <el-row class="ml10" v-show="showSearch">
        <el-form :inline="true" :model="state.queryForm" ref="queryRef">
          <el-form-item label="患者姓名" prop="customerName">
            <el-input placeholder="请输入患者姓名" v-model="state.queryForm.customerName"/>
          </el-form-item>

          <el-form-item label="社保卡" prop="medicalCard">
            <el-input v-model="state.queryForm.medicalCard" placeholder="请输入社保卡"/>
          </el-form-item>

          <el-form-item label="身份证号" prop="icCard">
            <el-input v-model="state.queryForm.icCard" placeholder="请输入身份证号"/>
          </el-form-item>

          <el-form-item label="就诊科室" prop="visitDept">
            <!--            <el-input v-model="state.queryForm.visitDept" placeholder="请输入就诊科室"/>-->
            <el-select v-model="state.queryForm.visitDept" placeholder="请选择就诊科室" clearable>
              <el-option :label="item" :value="item" v-for="(item, index) in deptList"
                         :key="index"/>
            </el-select>
          </el-form-item>

          <el-form-item label="医生工号" prop="doctorCode">
            <el-input v-model="state.queryForm.doctorCode" placeholder="请输入医生工号"/>
          </el-form-item>

          <el-form-item label="就诊医生" prop="doctorName">
            <el-input v-model="state.queryForm.doctorName" placeholder="请输入就诊医生"/>
          </el-form-item>

          <el-form-item label="查询时间">
            <el-date-picker
                v-model="queryFormData.detectionTime"
                type="daterange"
                format="YYYY-MM-DD"
                value-format="YYYY-MM-DD"
                range-separator="至"
                start-placeholder="开始时间"
                end-placeholder="结束时间"
            />
          </el-form-item>

          <el-form-item>
            <el-button @click="queryBtn" formDialogRef icon="search" type="primary">
              {{ $t('common.queryBtn') }}
            </el-button>
            <el-button @click="resetQuery" formDialogRef icon="Refresh">{{
                $t('common.resetBtn')
              }}
            </el-button>
          </el-form-item>
        </el-form>
      </el-row>

      <el-row>
        <div class="mb8" style="width: 100%">
          <el-button icon="folder-add" type="primary" class="ml10" @click="formDialogRef.openDialog()"
                     v-auth="'blood_bloodxCistomerDirection_add'">
            新 增
          </el-button>
          <el-button plain :disabled="multiple" icon="Delete" type="primary"
                     v-auth="'blood_bloodxCistomerDirection_del'" @click="handleDelete(selectObjs)">
            删除
          </el-button>
          <right-toolbar v-model:showSearch="showSearch" :export="'blood_bloodxCistomerDirection_export'"
                         @exportExcel="exportExcel" class="ml10 mr20" style="float: right;"
                         @queryTable="getDataList"></right-toolbar>
        </div>
      </el-row>
      <el-table
          :data="state.dataList" v-loading="state.loading" border
          :cell-style="tableStyle.cellStyle"
          :header-cell-style="tableStyle.headerCellStyle"
          @selection-change="selectionChangHandle"
          @sort-change="sortChangeHandle">
        <el-table-column type="selection" width="40" align="center"/>
        <el-table-column type="index" label="#" width="50"/>
        <el-table-column prop="doctorCode" label="医生工号" show-overflow-tooltip/>
        <el-table-column prop="doctorName" label="医生姓名" show-overflow-tooltip/>
        <el-table-column prop="visitDept" label="就诊科室" show-overflow-tooltip/>
        <el-table-column prop="customerName" label="患者姓名" show-overflow-tooltip/>
        <el-table-column prop="icCard" label="身份证号" show-overflow-tooltip/>
        <el-table-column prop="medicalCard" label="社保卡号" show-overflow-tooltip/>
        <el-table-column prop="visitTime" label="就诊时间" show-overflow-tooltip/>
        <el-table-column prop="visitEndTime" label="结束时间" show-overflow-tooltip/>
        <!--        <el-table-column label="操作" width="150">-->
        <!--          <template #default="scope">-->
        <!--            <el-button icon="edit-pen" text type="primary" v-auth="'blood_bloodxCistomerDirection_edit'"-->
        <!--                       @click="formDialogRef.openDialog(scope.row.id)">编辑-->
        <!--            </el-button>-->
        <!--            <el-button icon="delete" text type="primary" v-auth="'blood_bloodxCistomerDirection_del'"-->
        <!--                       @click="handleDelete([scope.row.id])">删除-->
        <!--            </el-button>-->
        <!--          </template>-->
        <!--        </el-table-column>-->
      </el-table>
      <pagination @size-change="sizeChangeHandle" @current-change="currentChangeHandle" v-bind="state.pagination"/>
    </div>

    <!-- 编辑、新增  -->
    <form-dialog ref="formDialogRef" @refresh="getDataList(false)"/>

  </div>
</template>

<script setup lang="ts" name="systemBloodxCistomerDirection">
import {BasicTableProps, useTable} from "/@/hooks/table";
import {fetchList, delObjs, getDeptListApi} from "/@/api/blood/bloodxCistomerDirection";
import {useMessage, useMessageBox} from "/@/hooks/message";


// 引入组件
const FormDialog = defineAsyncComponent(() => import('./form.vue'));
// 定义查询字典

// 定义变量内容
const formDialogRef = ref()
// 搜索变量
const queryRef = ref()
const showSearch = ref(true)
// 多选变量
const selectObjs = ref([]) as any
const multiple = ref(true)

const deptList = ref([])

const state: BasicTableProps = reactive<BasicTableProps>({
  queryForm: {},
  pageList: fetchList,

  descs: ['visit_time'],
})


const queryFormData = reactive({
  // 最近一年的时间
  detectionTime: [],
});
//  table hook
const {
  getDataList,
  currentChangeHandle,
  sizeChangeHandle,
  sortChangeHandle,
  downBlobFile,
  tableStyle
} = useTable(state)

// 清空搜索条件
const resetQuery = () => {
  queryFormData.detectionTime = []
  delete state.queryForm.startTime
  delete state.queryForm.endTime
  // 清空搜索条件
  queryRef.value?.resetFields()
  // 清空多选
  selectObjs.value = []
  getDataList()
}

onMounted(() => {
  getDeptList()
})

const getDeptList = () => {
  getDeptListApi().then((res) => {
    deptList.value = res.data
  })
}

const queryBtn = () => {
  if (queryFormData.detectionTime.length > 0) {
    // 判断是否存在 00:00:00  或者 23:59:59，存在则不处理，不存在则添加
    if (queryFormData.detectionTime[0].indexOf('00:00:00') === -1) {
      state.queryForm.startTime = queryFormData.detectionTime[0] + ' 00:00:00';
    }
    if (queryFormData.detectionTime[1].indexOf('23:59:59') === -1) {
      state.queryForm.endTime = queryFormData.detectionTime[1] + ' 23:59:59';
    }
  }
  getDataList()
}

// 导出excel
const exportExcel = () => {
  downBlobFile('/blood/bloodxCistomerDirection/export', Object.assign(state.queryForm, {ids: selectObjs}), 'bloodxCistomerDirection.xlsx')
}

// 多选事件
const selectionChangHandle = (objs: { id: string }[]) => {
  selectObjs.value = objs.map(({id}) => id);
  multiple.value = !objs.length;
};

// 删除操作
const handleDelete = async (ids: string[]) => {
  try {
    await useMessageBox().confirm('此操作将永久删除');
  } catch {
    return;
  }

  try {
    await delObjs(ids);
    getDataList();
    useMessage().success('删除成功');
  } catch (err: any) {
    useMessage().error(err.msg);
  }
};
</script>
